var form;
var table;
var layer;
layui.use(['layer','form','table'],function(){
    form = layui.form;
    table=layui.table;
    layer=layui.layer;

    //获取路由跳转过来的用户id
    var idArray = window.location.search.split("?");
    var userId = idArray[1];
    $("input[name='userId']").val(userId);


    selCouponUsed();
    selCouponUsedSelect();

});

//刷新
function refresh() {
    location.reload();
}

//返回
function go() {
    history.go(-1)
}

//获取用户优惠券记录明细
function selCouponUsed() {
    var userId= $("input[name='userId']").val();

    $.ajax({
        url: "/SyOperationController/selCouponUsed",
        type: "post",
        dataType: "json",
        data:{"user_id":userId},
        success: function (data) {
            $("#user_id").text(data.obj[0].user_id);
            $("#account").text(data.obj[0].account);
            $("#nickname").text(data.obj[0].nickname);
            $("#used").text(data.obj[0].used);
            $("#notUsed").text(data.obj[0].notUsed);
            $("#expired").text(data.obj[0].expired);
        }
    });
}

//查看用户优惠券记录表格数据
function selCouponUsedSelect() {

    var userId= $("input[name='userId']").val();
    var couponName=$("input[name='couponName']").val();
    var state=$("#state option:selected").val();
    var mode=$("#mode option:selected").val();
    var getDate=$("#getDate").val();
    var useDate=$("#useDate").val()
    var couponPrice=$("#couponPrice").val();


    $.ajax({
        url: "/SyOperationController/selCouponUsedSelect",
        type: "post",
        dataType: "json",
        data:{"userId":userId,"couponName":couponName,"state":state,"mode":mode,"getDate":getDate,"useDate":useDate,"couponPrice":couponPrice},
        success: function (data) {
            getTable(data.obj)
        }
    });
}

//优惠券数据列表
function getTable(data){
    var tb1=table.render({
        elem:"#table_id",
        title:"优惠券数据列表",
        limit:5,
        data:data,
        page:true,
        cols:[[
            {type:'checkbox',width:60},
            {field:'coupon_code',title:'优惠码',width:150,align:'center'},
            {field:'coupon_name',title:'优惠券名称',width:150,align:'center'},
            {field:'coupon_price',title:'面额',width:150,align:'center'},
            {field:'mode',title:'领取方式',width:150,align:'center',templet:function(d){
                    if(d.mode==2){
                        return  "<div>自动发放</div>"
                    }else if(d.mode==3){
                        return  "<div>活动领取</div>"
                    }else if(d.mode==4){
                        return  "<div>人工发放</div>"
                    }else if(d.mode==5){
                        return  "<div>无限制</div>"
                    }
                }},
            {field:'get_date',title:'领取时间',width:200,align:'center',templet:function(d){
                    var date=d.get_date;
                    date = date.slice(0, date.indexOf("."));
                    date = date.replaceAll('T',' ');
                    return date;
                }},
            {field:'state',title:'当前状态',width:150,align:'center',templet:function(d){
                    if(d.state==2){
                        return "已使用";
                    }else if(d.state==3){
                        return "未使用";
                    }else{
                        return "异常";
                    }
                }},
            {field:'use_date',title:'使用时间',width:200,align:'center',templet:function(d){
                    if(d.use_date){
                        var date=d.use_date;
                        date = date.slice(0, date.indexOf("."));
                        date = date.replaceAll('T',' ');
                        return date;
                    }else{
                        return "N/A";
                    }
                }},
            {field:'order_no',title:'订单编号',width:200,align:'center',templet:function(d){
                    if(d.order_no){
                        return d.order_no;
                    }else{
                        return "N/A";
                    }
                }},
            {field:'',title:'操作',width:150,align:'center',templet:function(d){
                    return "<span ><a  onclick='javascript:CouponDetailsSelelctText(\""+d.used_id+"\")' style='color:#1ABC9C;'>查看</a></span>"
                }}
        ]]
    });
}

//点击查检索页面
function retrieve() {

    //点击检索之后清空输入框
    $("input[name!='userId']").val('');
    $(this).find("option:selected").removeAttr("selected");

    layer.open({
        type : 2,
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>高级检索</div>",
        area : [ '670px', '500px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"CouponDetailsSelelctRetrieve.html",
        success:function(){

        }
    });
    window.layer_1=layer;
}


//查看
function CouponDetailsSelelctText(usedId){
    $.ajax({
        url: "/operation/selCouponUsedSelect",
        type: "post",
        dataType: "json",
        data:{"usedId":usedId},
        success: function (data) {
            CouponDetailsSelelctText2(data.obj[0])
        }
    })
}
//弹出查看详情页面
function  CouponDetailsSelelctText2(data) {
    layer.open({
        type : 2,
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>查看详情</div>",
        area : [ '720px', '375px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"CouponDetailsSelelctText.html",
        success:function(layero,index){

            //获取优惠券类型
            var type=null;
            if(data.type==2){
                type="商品";
            }else if(data.type==3){
                type="视频";
            }else if(data.type==2){
                type="全部赠券"
            }

            //获取领取方式
            var mode=null;
            if(data.mode==2){
                mode="自动发放";
            }else if(data.mode==3){
                mode="活动领取";
            }else if(data.mode==4){
                mode="人工发放";
            }else if(data.mode==5){
                mode="无限制";
            }

            //获取有效期
            var start=data.start_date;
            start = start.slice(0, start.indexOf("."));
            start = start.replaceAll('T',' ');


            var end=data.end_date
            end = end.slice(0, end.indexOf("."));
            end = end.replaceAll('T',' ');



            //领取时间
            var get_date=data.get_date;
            get_date = get_date.slice(0, get_date.indexOf("."));
            get_date = get_date.replaceAll('T',' ');



            //使用时间
            var use_date=data.use_date;
            use_date = use_date.slice(0, use_date.indexOf("."));
            use_date = use_date.replaceAll('T',' ');


            //获取当前状态
            var state=null;
            if(data.state==2){
                state="已使用";
            }else if(data.state==3){
                state="未使用";
            }

            //获取面值
            var coupon_price="";
            if(data.coupon_price!=null){
                coupon_price=data.coupon_price+"元";
            }

            var obj=layero.find('iframe').contents();
            obj.find('#coupon_code').text(data.coupon_code);
            obj.find('#coupon_name').text(data.coupon_name);
            obj.find('#type').text(type);
            obj.find('#coupon_price').text(coupon_price);
            obj.find('#mode').text(mode);
            obj.find('#date').html("<p>开始："+start+"</p>"+"<p>结束："+end+"</p>");
            obj.find('#get_date').text(get_date);
            obj.find('#use_date').text(use_date);
            obj.find('#state').text(state);
            obj.find('#order_no').text(data.order_no);

        }
    });
    window.layer_1=layer;
}

